package com.meida.module.bus.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.module.bus.client.entity.BusScholartyrant;
import com.meida.module.bus.provider.service.BusSubjectService;
import com.meida.module.user.client.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 后台学霸认证信息列表
 */
@Component
public class BusScholarTyrantPageListHandler implements PageInterceptor<BusScholartyrant> {

    @Autowired
    private BusSubjectService busSubjectService;

    @Override
    public void prepare(CriteriaQuery<BusScholartyrant> cq, PageParams pageParams, EntityMap params) {
        cq.clear();
        String beginDate = params.get("beginDate");
        String endDate = params.get("endDate");
        cq.select(BusScholartyrant.class, "scholartyrantId", "subjectInfo", "selfIntroduction", "tags", "state", "approvalInfo", "createTime");
        cq.select(AppUser.class, "userName");
        cq.eq(BusScholartyrant.class, "state");
        cq.isNotNull("state");
        cq.eq(BusScholartyrant.class, "scholartyrantId");
        cq.like(AppUser.class, "userName");
        cq.ge("DATE_FORMAT(scholartyrant.createTime,'%Y-%m-%d')", beginDate);
        cq.le("DATE_FORMAT(scholartyrant.createTime,'%Y-%m-%d')", endDate);
        cq.createJoin(AppUser.class).setMainField("scholartyrantId").setJoinField("userId");
    }

    @Override
    public void complete(CriteriaQuery<BusScholartyrant> cq, List<EntityMap> result, EntityMap extra) {
        result.forEach(entityMap -> {
            String subjectIds = entityMap.get("subjectInfo");
            List<String> subjectTitles = busSubjectService.selectSubjectTitles(subjectIds);
            entityMap.put("subjectTitles", subjectTitles);
        });
    }


}
